package homework;

import java.util.Stack;

//4.逆波兰表达式求值
//
//作业内容
//逆波兰表达式求值(仅上传代码即可)。
//https://leetcode.cn/problems/evaluate-reverse-polish-notation/description/
public class h1789 {
    static class Solution {
        public int evalRPN(String[] tokens) {
            Stack<Integer> stack = new Stack<>();
            for(String str : tokens)
            {
                if(str.equals("+"))
                {
                    int b = stack.pop();
                    int a = stack.pop();
                    stack.push(a + b);
                }
                else if(str.equals("-"))
                {
                    int b = stack.pop();
                    int a = stack.pop();
                    stack.push(a - b);
                }
                else if(str.equals("*"))
                {
                    int b = stack.pop();
                    int a = stack.pop();
                    stack.push(a * b);
                }
                else if(str.equals("/"))
                {
                    int b = stack.pop();
                    int a = stack.pop();
                    stack.push(a / b);
                }
                else
                {
                    stack.push(Integer.valueOf(str));
                }
            }
            return stack.pop();
        }
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        System.out.println(solution.evalRPN(new String[]{"10","6","9","3","+","-11","*","/","*","17","+","5","+"}));
    }
}
